<!--
  author: tangcheng_cd
  create by: 2018/5/23
  descr: 用户信息表格
-->
<template>
  <div class="user-grid-comp">
    <el-row>
      <slot name="toolbar"></slot>
      <el-input
        size="mini"
        placeholder="输入账号或姓名"
        auto-complete="on"
        v-model="search.keyword"
        class="user-grid-search-input pull-right"
      >
        <el-button type="primary"
                   @click="getData"
                   slot="append"
                   icon="el-icon-search"></el-button>
      </el-input>
    </el-row>
    <el-table
      ref="userGrid"
      v-loading="tableLoading"
      @selection-change="handleGridTableSelection"
      :data="pageData.list"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="45">
      </el-table-column>
      <el-table-column
        prop="code"
        label="账号">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="search.pageNo"
      :page-sizes="[5, 10, 15]"
      :page-size="search.pageSize"
      layout="prev, pager, next, jumper"
      :total="pageData.total">
    </el-pagination>
  </div>
</template>

<script type="text/ecmascript-6">
import api from '../../../api/url'
import ajax from '../../../api/ajax'

export default {
  name: 'user-grid',
  data () {
    return {
      tableLoading: false, // 数据是否加载中
      search: {
        type: Object,
        pageNo: 1, // 当前页号
        pageSize: 5, // 每页显示记录数
        keyword: null // 搜索关键字
      },
      pageData: Object, // 当前表格页数据
      multipleSelection: [] // 表格中选择的数据
    }
  },
  methods: {
    // 页记录数更改
    handleSizeChange (pageSize) {
      this.search.pageNo = 1 // 重置到第一页
      this.search.pageSize = pageSize
      this.getData()
    },
    // 当前页切换
    handleCurrentChange (pageNo) {
      this.search.pageNo = pageNo
      this.getData()
    },
    // 获取页面数据
    getData () {
      // 角色数据加载中
      this.tableLoading = true
      ajax.post(api.SEARCHSYSUSER, this.search).then(res => {
        this.pageData = res.data
        // 角色数据加载完毕
        this.tableLoading = false
      }).catch(e => {
        console.error('获取用户数据发生错误！')
      })
    },
    // 当表格被选中
    handleGridTableSelection (val) {
      this.multipleSelection = val
    },
    // 获取所有的选择记录
    getSelectionRecords () {
      return this.multipleSelection
    }
  }
}
</script>

<style lang="stylus" rel="stylesheet/stylus">
.user-grid-comp
  .user-grid-search-input
    margin-right 10px
    width 200px
</style>
